package com.mifan.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mifan.common.Results;
import com.mifan.entity.Menu;
import com.mifan.mapper.MenuMapper;
import com.mifan.vo.ResultData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Milan
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询菜单列表
     * @return
     */
    @GetMapping("/list")
    public Results list() {
        return getResults();
    }




    /**
     * 更新菜单菜单
     * @param menu
     * @return
     */
    @PostMapping("save")
    public Results save(@RequestBody  Menu menu) {
        System.out.println(menu);
        int result = menuMapper.updateById(menu);
        if (result > 0) {
            return Results.success(null);
        }
        return Results.failure("更新失败");
    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @PostMapping("add")
    public Results add(@RequestBody  Menu menu) {
        System.out.println(menu);
        int result = menuMapper.insert(menu);
        if (result > 0) {
            return Results.success(null);
        }
        return Results.failure("添加失败");
    }


    /**
     * 查询当前用户所拥有的菜单
     * @param
     * @return
     */
    @GetMapping("/menuWithRole")
    public Results menuWithRole(@RequestParam Long roleid) {
        if(roleid == 1){
            return getResults();
        }
        List<Menu> menuListByRoleId = menuMapper.getMenuListByRoleId(roleid);
        return Results.success(menuListByRoleId);
    }



    // TODO 树形结构的菜单列表
    @GetMapping("/tree")
    public Results tree() {
       return getResults();
    }



    /**
     * TODO 获取所有菜单列表不分页
     * @return
     */
    private Results getResults() {
        List<Menu> menusM = menuMapper.getMenuListByTypeMAndSort();
        List<Menu> menusC = menuMapper.getMenuListByTypeCAndSort();
        List<Menu> combinedMenus = new ArrayList<>();
        combinedMenus.addAll(menusM);
        combinedMenus.addAll(menusC);
        return Results.success(combinedMenus);
    }
}
